package cn.jy.erp.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import cn.jy.erp.domain.mobile.Partsmade;
import cn.jy.erp.domain.mobile.PartsmadeDetail;
import cn.jy.erp.service.MobileZzmenuService;
import cn.jy.erp.util.JsonReplace;

@Controller
public class ZzmenuController {
	@Autowired
	MobileZzmenuService service;
	@RequestMapping("/getZzdetail")
	@ResponseBody
	public String Zzdetail(HttpServletRequest request,HttpServletResponse response) throws Exception{
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		String callback = request.getParameter("callback");
		if (callback != null && !"".equals(callback)) {
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(service.selectMenu());
			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = response.getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
		return null;

	}
	@RequestMapping("/getById")
	@ResponseBody
	public String getById(HttpServletRequest request,HttpServletResponse response) throws Exception{
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		String callback = request.getParameter("callback");
		String id = request.getParameter("tid");
		String did = request.getParameter("did");
		if (callback != null && !"".equals(callback)&& did!=null&&!did.endsWith("")) {
			String json = JsonReplace.getJson(service.selectByTidDid(id,did));
			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = response.getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}else if(callback != null && !"".equals(callback)){
			String json = JsonReplace.getJson(service.selectByTid(id));
			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = response.getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
		return null;
		}
	
	@RequestMapping("/saveorupdateZz")
	@ResponseBody
	public String saveorupdateZz(HttpServletRequest request,HttpServletResponse response) throws Exception{
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		String callback = request.getParameter("callback");
		String tid = request.getParameter("tid");
		String did = request.getParameter("did");
		String detailname = request.getParameter("detailname");
		String batchnums = request.getParameter("batchnums");
		if (batchnums!=null) {
			batchnums=new String(batchnums.getBytes("ISO-8859-1"), "UTF-8");
		}
		String type = request.getParameter("type");
		String planNums = request.getParameter("plannums");
		String planunit = request.getParameter("planunit");
		String planboxnums = request.getParameter("planboxnums");
		String customer = request.getParameter("customer");
		String begintime = request.getParameter("begintime");
		String beginaddtime = request.getParameter("beginaddtime");
		String needer = request.getParameter("needer");
		needer=needer.replace("年", "-");
		needer=needer.replace("月", "-");
		needer=needer.replace("日", "");
		String atoperator = request.getParameter("atoperator");
		String needunit = request.getParameter("needunit");
		String needboxnums = request.getParameter("needboxnums");
		String starttime = request.getParameter("starttime");
		String neednums = request.getParameter("neednums");
		String overaddtime = request.getParameter("overaddtime");
		overaddtime=overaddtime.replace("年", "-");
		overaddtime=overaddtime.replace("月", "-");
		overaddtime=overaddtime.replace("日", "");
		PartsmadeDetail t =new PartsmadeDetail();
		t.setTid(tid);
		t.setDid(did);
		t.setBatchnums(batchnums);
		t.setDetailname(detailname);
		t.setType(type);
		t.setPlannums(planNums);
		t.setPlanunit(planunit);
		if(!planboxnums.equals("")){
		t.setPlanboxnums(planboxnums);}
		if(!customer.equals("")){
		t.setCustomer(customer);}
		t.setBegintime(begintime);
		t.setBeginaddtime(beginaddtime);
		t.setNeeder(needer);
		t.setAtoperator(atoperator);
		if(!needunit.equals("")){
		t.setNeedunit(needunit);
		}
		if(!needboxnums.equals("")){
		t.setNeedboxnums(needboxnums);
		}
		t.setStarttime(starttime);
		t.setNeednums(neednums);
		if(!overaddtime.endsWith("")){
		t.setOveraddtime(overaddtime);}
		if (callback != null && !"".equals(callback)) {
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(service.saveorupdate(t));
			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = response.getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
		return service.saveorupdate(t);
	}
}
